package com.example.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.ApiResult;
import com.example.entity.Video;
import com.example.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "视频管理")
@RequestMapping("/api/backend/video")
public class BackendVideoController {

    @Resource
    private VideoService videoService;


    @PutMapping("/thumbVideo/{videoId}")
    @ApiOperation("点赞")
    public ApiResult thumbVideo(@PathVariable Integer videoId){
        Integer integer = videoService.thumbVideo(videoId);
        if (integer == 1){
            return ApiResult.ok(integer);
        }else {
            return ApiResult.fail("点赞失败！");
        }
    }

    @ApiOperation("上传视频")
    @PostMapping("/addVideo")
    public ApiResult addVideo(@RequestParam String  videoTitle,
                              @RequestPart MultipartFile videoUrl,
                              @RequestParam Integer courseId,
                              HttpServletRequest request){
        Integer integer = videoService.addVideo(videoTitle, videoUrl, courseId,request);
        if (integer == 1){
            return ApiResult.ok(integer);
        }else {
            return ApiResult.fail("上传失败！");
        }
    }
    @ApiOperation("模糊分页查询")
    @GetMapping("/like/{name}/{page}/{size}")
    public ApiResult likeAndPage(@PathVariable String  name,
                                 @PathVariable Integer page,
                                 @PathVariable Integer size){
        Page<Video> videoPage = videoService.selectVideoPage(name, page, size);
        return ApiResult.ok(videoPage);
    }

    @ApiOperation("删除")
    @DeleteMapping("/deleteById")
    public ApiResult deleteById(@RequestParam Integer id){
        Integer integer = videoService.deleteById(id);
        if (integer == 1){
            return ApiResult.ok(integer);
        }else {
            return ApiResult.fail("删除失败！");
        }
    }
}
